package cn.lx.es.query.processor;

import cn.lx.es.query.annotation.QueryStringSearch;
import cn.lx.es.query.annotation.SearchProcessor;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.lang.reflect.Field;

/**
 * 内置处理器 - 查询字符串处理器
 */
@SearchProcessor
public class SearchQueryStringProcessor implements SearchAnnotationProcessor<QueryStringSearch> {
    @Override
    public void process(QueryStringSearch annotation, Field field, Object value, BoolQueryBuilder boolQueryBuilder) {
        if (value != null) {
            String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
            boolQueryBuilder.must(QueryBuilders.queryStringQuery(value.toString()).field(fieldName));
        }
    }
    @Override
    public Class<QueryStringSearch> getAnnotationType() {
        return QueryStringSearch.class;
    }
}
